package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.promotion.storage.UploadPicStorageVO;
import com.cskaoyan.service.AdministratorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Description:
 * @Author: WD created on 2021-09-20 11:55
 * @Version:
 */
@RequestMapping("wx/storage")
@RestController
public class WXStorageController {
    @Autowired
    AdministratorsService administratorsService;

    @SuppressWarnings("all")
    @RequestMapping("upload")
    public BaseRespVo uploadPic(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return BaseRespVo.fail("上传文件为空");
        }

        String type = file.getContentType();
        //判断后缀
        if (!("image/png".equals(type) || "image/jpg".equals(type) || "image/jpeg".equals(type))) {
            return BaseRespVo.fail("必须上传png或jpg文件！");
        }
        //判断大小
        if (file.getSize() > 1024 * 1024) {
            return BaseRespVo.fail("文件大小超出限制！");
        }

        UploadPicStorageVO storageVO = administratorsService.setPic(file);

        return BaseRespVo.ok(storageVO);

    }


}
